Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add matplotlib style configuration for circuit drawing #1811

Merged
merged 61 commits into from
Dec 3, 2021
Merged

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Oct 27, 2021

The new matplotlib circuit drawing characteristics make very few styling choices for users, instead relying on matplotlib configuration. This gives a great deal more flexibility for users to tune things to their own personal preferences.

But the default style doesn't always look the best, and individual fine tuning can take a lot of time and effort.

Therefore this PR adds (at least one) function that will modify plt.rcParams for preconfigured styles.

Styles can be stored in *.mplstyle data files, but I do not know how to best ship pennylane with stylesheets in a convenient way. Therefore, this PR just alters plt.rcParams directly.

qml.circuit_drawer.black_white_style()

image

@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@codecov
Copy link

codecov bot commented Oct 27, 2021

Codecov Report

Merging #1811 (386e9b7) into master (23b82a4) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1811   +/-   ##
=======================================
  Coverage   98.81%   98.81%           
=======================================
  Files         226      227    +1     
  Lines       17233    17264   +31     
=======================================
+ Hits        17028    17059   +31     
  Misses        205      205           
Impacted Files Coverage Δ
pennylane/drawer/mpldrawer.py 98.66% <ø> (ø)
pennylane/drawer/tape_mpl.py 100.00% <ø> (ø)
pennylane/transforms/draw.py 100.00% <ø> (ø)
pennylane/drawer/__init__.py 100.00% <100.00%> (ø)
pennylane/drawer/style.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 23b82a4...386e9b7. Read the comment docs.

@albi3ro albi3ro added the WIP 🚧 Work-in-progress label Oct 27, 2021
@albi3ro albi3ro removed the WIP 🚧 Work-in-progress label Nov 17, 2021
Copy link
Contributor

@Jaybsoni Jaybsoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just some small things! Out of curiosity, can we have a black white and green style?

doc/_static/styles/styles_examples.py Outdated Show resolved Hide resolved

if __name__ == "__main__":
black_white_style_example()
black_white_style_dark_example()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a newline for the end of the file (formatting)

Suggested change
black_white_style_dark_example()
black_white_style_dark_example()

Comment on lines 24 to 25
except (ModuleNotFoundError, ImportError) as e:
has_mpl = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice check,
Codecov is complaining that these lines aren't being tested. Not sure how you would test them since you don't have access to the environment during the test.


"""

if not has_mpl: # pragma: no cover
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is pragma?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excludes the line for code coverage. I'll just have to add this above as well.

pennylane/circuit_drawer/styles.py Outdated Show resolved Hide resolved
pennylane/circuit_drawer/styles.py Outdated Show resolved Hide resolved
@albi3ro albi3ro requested a review from Jaybsoni December 2, 2021 16:34
Comment on lines +15 to +24
This module contains styles for using matplotlib graphics.

To add a new style:
* create a private function that modifies ``plt.rcParams``.
* add an entry to the private dictionary ``_style_map``.
* Add an entry to ``doc/code/qml.drawer.rst``
* Add a test in ``tests/drawer/test_style.py``

Use the decorator ``_needs_mpl`` on style functions to raise appropriate
errors if ``matplotlib`` is not installed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we place this information somewhere more user facing? Like in the docs somewhere ?
I think it would be helpful to developers who might just read the docs and not look at the source code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this information should be "user facing". If a developer (or external contributor) is wanting to create a new style, this should be the first file they look at anyway.

rcparams(circuit)
Solarize_Light2(circuit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rip solarize_light2

Comment on lines 293 to 295
You can globally control the style with ``plt.rcParams`` and styles
If we customize ``plt.rcParams``, we get a
different style:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wording sounded a little weird. Is styles a separate parameter used with plt.rcParams? If not I think this suggestion sounds better:

Suggested change
You can globally control the style with ``plt.rcParams`` and styles
If we customize ``plt.rcParams``, we get a
different style:
You can globally control the style with ``plt.rcParams``.
If we customize ``plt.rcParams``, we get a different style:

assert plt.rcParams["lines.color"] == "black"
assert plt.rcParams["text.color"] == "black"

plt.style.use("default")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to reset it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want it interfering with potential other tests. These changes are global and so would affect later tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure !

assert plt.rcParams["lines.color"] == "white"
assert plt.rcParams["text.color"] == "white"

plt.style.use("default")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same as above)

Copy link
Contributor

@Jaybsoni Jaybsoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Just small documentation points, but other then that happy to approve

pennylane/drawer/tape_mpl.py Outdated Show resolved Hide resolved
Comment on lines 207 to 210
You can also control the style with ``plt.rcParams`` and styles, see the
`matplotlib docs <https://matplotlib.org/stable/tutorials/introductory/customizing.html>`_ .
If we customize ``plt.rcParams``, we get a
different style:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wording sounded a little weird. Is styles a separate parameter used with plt.rcParams? If not I think this suggestion sounds better:

Suggested change
You can also control the style with ``plt.rcParams`` and styles, see the
`matplotlib docs <https://matplotlib.org/stable/tutorials/introductory/customizing.html>`_ .
If we customize ``plt.rcParams``, we get a
different style:
You can also control the style with ``plt.rcParams``, see the
`matplotlib docs <https://matplotlib.org/stable/tutorials/introductory/customizing.html>`_ .
If we customize ``plt.rcParams``, we get a different style:

@albi3ro albi3ro requested a review from Jaybsoni December 3, 2021 12:04
Copy link
Contributor

@Jaybsoni Jaybsoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome looks good! Glad to have these styles in 👍🏼

@albi3ro albi3ro merged commit da8652a into master Dec 3, 2021
@albi3ro albi3ro deleted the drawer_styles branch December 3, 2021 22:44
@antalszava
Copy link
Contributor

[sc-12056]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants